#include "movement_line.h"

namespace animationagent {

MovementLine::MovementLine(Sint16 ox, Sint16 oy, Sint16 dx, Sint16 dy, Uint32 time_msec)
{
    orig_x = ox;
    orig_y = oy;
    dest_x = dx;
    dest_y = dy;
    distance_x = dx - ox;
    distance_y = dy - oy;
    //endtime_msec = misc::get_m_seconds() + time_msec;
    movetime_msec = time_msec;
    starttime_msec = oldtime_msec = SDL_GetTicks();
    endtime_msec = starttime_msec + time_msec;
    undermoving = true;
}

MovementLine::~MovementLine()
{
}

bool MovementLine::update(SDL_Rect &rect)
{
    bool dirty = false;
    if (undermoving) {
        if (oldtime_msec < endtime_msec) {
            double time;
            oldtime_msec = SDL_GetTicks();
            time = ((float)(oldtime_msec - starttime_msec)) / movetime_msec;
            time = time > 1.0 ? 1.0 : time;
            rect.x = orig_x + (Sint16)(distance_x * time);
            rect.y = orig_y + (Sint16)(distance_y * time);
        } else {
            rect.x = dest_x;
            rect.y = dest_y;
            undermoving = false;

#if 0
            SDL_Event event;

            event.type = SDL_USEREVENT;
            event.user.code = ON_MOVE_FINISH;
            event.user.data1 = this->objectID;
            event.user.data2 = NULL;
            SDL_PushEvent(&event);
#endif
        }
        dirty = true;
    }
    return dirty;
}



}
